<?php

namespace app\airobot\controller;

use app\airobot\CommonLogic;
use app\airobot\model\OriginBattleConfig;
use app\airobot\model\OriginRobotSpinConfig;
use think\admin\Controller;
use think\facade\Db;
use ZipArchive;

/**
 * 超管系统-结果统计
 * Class Banner
 * @package app\airobot\controller
 */
class BattleResultRecords extends Controller
{
    private $table = 'battle_result_records';
    public $commonLogic;

    /**
     * 结果统计
     * @auth true
     */
    public function index()
    {
        $this->commonLogic = new CommonLogic();
        $this->title = '结果统计';
        $query = $this->_query($this->table);
        $where = [];
        if (!empty($_GET['battle_id'])) {
            $where['battle_id'] = $_GET['battle_id'];
        }
        $query->where($where);
        $query->order('id desc')->page();
    }

    protected function _index_page_filter(array &$data)
    {
        $this->commonLogic = new CommonLogic();
        foreach ($data as &$vo) {
           /* $vo["battle_rtp"] = number_format($vo["battle_rtp"], 4);
            $vo["all_win"] = number_format($vo["all_win"], 4);
            $vo["all_real_bet"] = number_format($vo["all_real_bet"], 4);
            $vo["reduce_max"] = number_format($vo["reduce_max"], 4);
            $vo["battle_start_pool_amount"] = number_format($vo["battle_start_pool_amount"], 4);
            $vo["battle_end_pool_amount"] = number_format($vo["battle_end_pool_amount"], 4);
            $vo["battle_transfer_pool_amount"] = number_format($vo["battle_transfer_pool_amount"], 4);*/

            $vo["battle_rtp"] = $this->commonLogic->DealAmount($vo['battle_rtp']);
            $vo["all_win"] = $this->commonLogic->DealAmount($vo['all_win']);
            $vo["all_real_bet"] = $this->commonLogic->DealAmount($vo['all_real_bet']);
            $vo["reduce_max"] = $this->commonLogic->DealAmount($vo['reduce_max']);
            $vo["battle_start_pool_amount"] = $this->commonLogic->DealAmount($vo['battle_start_pool_amount']);
            $vo["battle_end_pool_amount"] = $this->commonLogic->DealAmount($vo['battle_end_pool_amount']);
            $vo["battle_transfer_pool_amount"] = $this->commonLogic->DealAmount($vo['battle_transfer_pool_amount']);
        }
    }

    /**
     * 查看结果
     * @auth true
     */
    public function querywin()
    {
        $battleResultRecordsModel = new \app\airobot\model\BattleResultRecords();
        $vo = $battleResultRecordsModel->queryInfo(["id" => $_GET["id"]]);
        if ($vo['win_user'] == "") {
            $this->success("错误！");
        }

        // 将 JSON 字符串解析为 PHP 对象或数组
        $paramsJson = json_decode($vo['win_user'], true);
        $formattedJsonString = json_encode($paramsJson, JSON_PRETTY_PRINT);
        $vo['result_json'] = $formattedJsonString;

        $this->vo = $vo;
        OriginBattleConfig::mForm('result');
    }

    /**
     * 查看结果
     * @auth true
     */
    public function queryresult()
    {
        $battleResultRecordsModel = new \app\airobot\model\BattleResultRecords();
        $vo = $battleResultRecordsModel->queryInfo(["id" => $_GET["id"]]);
        if ($vo['user_data'] == "") {
            $this->success("错误！");
        }

        // 将 JSON 字符串解析为 PHP 对象或数组
        $paramsJson = json_decode($vo['user_data'], true);
        // 使用 usort 函数按照 UserWeight 值进行升序排列
        usort($paramsJson, function ($a, $b) {
            return floatval($a['UserWeight']) - floatval($b['UserWeight']);
        });
        $formattedJsonString = json_encode($paramsJson, JSON_PRETTY_PRINT);
        $vo['result_json'] = $formattedJsonString;

        $this->vo = $vo;
        OriginBattleConfig::mForm('result');
    }
}